package com.gongpingjialoan.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.gongpingjialoan.R;
import com.gongpingjialoan.bean.PriceBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/4/12.
 */
public class PriceChangeView extends View {

    private static final float LINE_SMOOTHNESS = 18f;

    private int height, width;

    private double maxPrice;

    private boolean isDrawYear = false;

    //区域的画笔
    private Paint fillPaint;
    //小圆的半径
    private float circleR;

    //曲线画笔
    private Paint cubicPaint;

    //曲线虚线画笔
    private Paint cubicDashPaint;

    private Paint linePaint;
    //第一个坐标
    private PointF fristPointF;
    //第一个最下面的坐标
    private PointF fristEndPointF;

    //每格多少万，共5格
    private float perPrice;

    //每格的高度
    private float perHeight;

    //每格的宽度
    private float perWidth;

    private List<PriceBean> beans;

    private Paint dashPaint;

    //坐标画笔
    private Paint circlePaint;

    //最上面的高度最下面的高度
    private float topButtomHeight;

    //价格文字画笔
    private Paint textPaint;


    //坐标的个数
    private int size;

    //虚线的高度
    private float perDashLineHeight;

    public PriceChangeView(Context context) {
        this(context, null);
    }

    public PriceChangeView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PriceChangeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        height = getMeasuredHeight();
        width = getMeasuredWidth();
        perHeight = (height - 2 * topButtomHeight) / 5;
    }

    private void init() {
        topButtomHeight = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 35, getResources().getDisplayMetrics());
        perDashLineHeight = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics());
        // fristperWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30, getResources().getDisplayMetrics());
        circleR = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3, getResources().getDisplayMetrics());

        textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setTextSize(getResources().getDimensionPixelSize(R.dimen.text_smaller));
        textPaint.setColor(0xff4A576C);

        fillPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        fillPaint.setColor(0x88EFF5FC);

        linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        linePaint.setColor(0xff7A93AE);
        linePaint.setStrokeWidth(perDashLineHeight);
        linePaint.setStyle(Paint.Style.STROKE);

        circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        circlePaint.setStyle(Paint.Style.STROKE);
        circlePaint.setStrokeWidth(perDashLineHeight * 1.5f);
        circlePaint.setColor(0xff343F4B);

        cubicPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        cubicPaint.setColor(0xff4A576C);
        cubicPaint.setStrokeWidth(perDashLineHeight * 2f);
        cubicPaint.setStyle(Paint.Style.STROKE);

        dashPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        PathEffect effects = new DashPathEffect(new float[]{5, 5, 5, 5}, 1);
        dashPaint.setPathEffect(effects);
        dashPaint.setColor(0xffC1D2DA);
        dashPaint.setStyle(Paint.Style.STROKE);
        dashPaint.setStrokeWidth(perDashLineHeight);


        cubicDashPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        cubicDashPaint.setColor(0xff4A576C);
        cubicDashPaint.setPathEffect(effects);
        cubicDashPaint.setStrokeWidth(perDashLineHeight * 2f);
        cubicDashPaint.setStyle(Paint.Style.STROKE);


        initData();
    }

    //初始化数据
    private void initData() {
        beans = new ArrayList<>();
        beans.add(new PriceBean(20.0, 15, 11, false));
        maxPrice = beans.get(0).getPrice();
        for (PriceBean bean : beans) {
            if (bean.getPrice() > maxPrice) {
                maxPrice = bean.getPrice();
            }
        }
        //每格价格多少钱
        perPrice = (float) Math.ceil(maxPrice / 5);
    }

    public void setData(List<PriceBean> priceBeans){
        beans = priceBeans;
        if(priceBeans.isEmpty()) return;
        maxPrice = beans.get(0).getPrice();
        for (PriceBean bean : beans) {
            if (bean.getPrice() > maxPrice) {
                maxPrice = bean.getPrice();
            }
        }
        //每格价格多少钱
        if(maxPrice % 5 == 0){
            perPrice = (float) (maxPrice / 5);
        }else{
            if(maxPrice > 5){
                perPrice = (float) Math.ceil(maxPrice / 5);
            }else{
                perPrice = (float) (maxPrice * 0.2f);
            }
        }

        invalidate();
    }



    //计算坐标
    private void calculationPoint() {
        fristPointF = new PointF();
        fristEndPointF = new PointF();
        size = beans.size();
        perWidth = width / size;
        for (int i = 0; i < size; i++) {
            double price = beans.get(i).getPrice();
            //在第几格
            int index = (int) Math.ceil(price / perPrice);
//           Log.d("MyView", "第几" + index + "格");
            PointF point = new PointF();

            float offest = (float) (((perPrice * index) - price) / perPrice * perHeight);
            point.y = (5 - index) * perHeight + offest + topButtomHeight;
//          Log.d("MyView", "高度" + point.y);
            point.x = i * perWidth + perWidth / 2;
//            Log.d("MyView", "宽度" + point.x);
            beans.get(i).setPoint(point);
            PointF endPoint = new PointF();
            endPoint.x = point.x;
            endPoint.y = height - perDashLineHeight / 2 - topButtomHeight;
            beans.get(i).setEndPointF(endPoint);

            if (i == 0) {
                fristPointF.x = 0;
                fristPointF.y = point.y - 20;
                fristEndPointF.y = height - perDashLineHeight / 2 - topButtomHeight;
                fristEndPointF.x = 0;
            }
        }

    }


    @Override
    protected void onDraw(Canvas canvas) {
        calculationPoint();
        //画5条曲线
        for (int i = 0; i < 5; i++) {
            Path path = new Path();
            path.moveTo(0, perHeight * i + perDashLineHeight / 2 + topButtomHeight);
            path.lineTo(width, perHeight * i + perDashLineHeight / 2 + topButtomHeight);
            canvas.drawPath(path, dashPaint);
        }

        for (int i = -1; i < size - 1; i++) {
            float controlX, controlY;
            Path path = new Path();
            Path fillPath = new Path();
            if (i == -1) {
                path.moveTo(fristPointF.x, fristPointF.y);
                controlX = logicX(beans.get(0).getPoint().x, fristPointF.x);
                controlY = logicY(beans.get(0).getPoint().y, fristPointF.y, -1);
                path.quadTo(controlX, controlY, beans.get(0).getPoint().x, beans.get(0).getPoint().y);

                fillPath.moveTo(fristPointF.x, fristPointF.y + perDashLineHeight * 2f);
                fillPath.quadTo(controlX, controlY, beans.get(0).getPoint().x, beans.get(0).getPoint().y + perDashLineHeight * 2f);
                fillPath.lineTo(beans.get(0).getEndPointF().x, beans.get(0).getEndPointF().y);
                fillPath.lineTo(fristEndPointF.x, fristEndPointF.y);

                if (beans.get(0).isDash()) {
                    canvas.drawPath(path, cubicDashPaint);
                    fillPaint.setColor(0xaaDAE1ED);
                    canvas.drawPath(fillPath, fillPaint);
                } else {
                    canvas.drawPath(path, cubicPaint);
                    fillPaint.setColor(0x88EFF5FC);
                    canvas.drawPath(fillPath, fillPaint);
                }
            } else {
                path.moveTo(beans.get(i).getPoint().x, beans.get(i).getPoint().y);
                controlX = logicX(beans.get(i + 1).getPoint().x, beans.get(i).getPoint().x);
                controlY = logicY(beans.get(i + 1).getPoint().y, beans.get(i).getPoint().y, i % 2 == 0 ? 1 : -1);
                path.quadTo(controlX, controlY, beans.get(i + 1).getPoint().x, beans.get(i + 1).getPoint().y);

                fillPath.moveTo(beans.get(i).getPoint().x, beans.get(i).getPoint().y + perDashLineHeight * 2f);
                controlX = logicX(beans.get(i + 1).getPoint().x, beans.get(i).getPoint().x);
                controlY = logicY(beans.get(i + 1).getPoint().y, beans.get(i).getPoint().y, i % 2 == 0 ? 1 : -1);
                fillPath.quadTo(controlX, controlY, beans.get(i + 1).getPoint().x, beans.get(i + 1).getPoint().y + perDashLineHeight * 2f);
                fillPath.lineTo(beans.get(i + 1).getEndPointF().x, beans.get(i + 1).getEndPointF().y);
                fillPath.lineTo(beans.get(i).getEndPointF().x, beans.get(i).getEndPointF().y);
                fillPath.lineTo(beans.get(i).getPoint().x, beans.get(i).getPoint().y + perDashLineHeight * 2f);
//                fillPath.close();

                if (beans.get(i + 1).isDash()) {
                    canvas.drawPath(path, cubicDashPaint);
                    fillPaint.setColor(0xaaDAE1ED);
                    canvas.drawPath(fillPath, fillPaint);
                } else {
                    canvas.drawPath(path, cubicPaint);
                    fillPaint.setColor(0x88EFF5FC);
                    canvas.drawPath(fillPath, fillPaint);
                }
            }


            try {
                if (beans.get(i + 1).isDash() && !isDrawYear) {
                    float x = (beans.get(i).getEndPointF().x + beans.get(i + 1).getEndPointF().x) * 0.5f;
                    float y;
                    if (beans.get(i + 1).getPoint().y > beans.get(i).getPoint().x) {
                        y = (beans.get(i + 1).getPoint().y + beans.get(i + 1).getEndPointF().y) * 0.5f;
                    } else {
                        y = (beans.get(i).getPoint().y + beans.get(i).getEndPointF().y) * 0.5f;
                    }
                    float w = textPaint.measureText("一年后") / 2;
                    canvas.drawText("一年后", x - w, y + textPaint.getTextSize() / 2, textPaint);
                    isDrawYear = true;
                }
            } catch (Exception e) {
                isDrawYear = true;
            }
        }

        //先画大坐标圆圈以及文字
        //画最底部的年份
        for (int i = 0; i < size; i++) {
            circlePaint.setStyle(Paint.Style.STROKE);
            circlePaint.setColor(0xff343F4B);
            canvas.drawLine(beans.get(i).getPoint().x, beans.get(i).getPoint().y, beans.get(i).getEndPointF().x, beans.get(i).getEndPointF().y, linePaint);
            canvas.drawCircle(beans.get(i).getPoint().x, beans.get(i).getPoint().y, circleR, circlePaint);
            float w = textPaint.measureText(beans.get(i).getPrice() + "万");
            canvas.drawText(beans.get(i).getPrice() + "万", beans.get(i).getPoint().x - w / 2, beans.get(i).getPoint().y - topButtomHeight / 2, textPaint);

            String year = beans.get(i).getYear() + "年";
            w = textPaint.measureText(year);



            if (beans.get(i).getMouth() > 0) {
                canvas.drawText(year, beans.get(i).getEndPointF().x - w / 2, beans.get(i).getEndPointF().y + topButtomHeight / 2, textPaint);
                String mouth = beans.get(i).getMouth() + "月";
                w = textPaint.measureText(mouth);
                canvas.drawText(mouth, beans.get(i).getEndPointF().x - w / 2, beans.get(i).getEndPointF().y + textPaint.getTextSize() * 1.2f + topButtomHeight / 2, textPaint);
            }else{
                canvas.drawText(year, beans.get(i).getEndPointF().x - w / 2, beans.get(i).getEndPointF().y + topButtomHeight / 2 + textPaint.getTextSize()/2 , textPaint);
            }

        }
        //先画小坐标圆圈
        for (int i = 0; i < size; i++) {
            circlePaint.setColor(0xffffffff);
            circlePaint.setStyle(Paint.Style.FILL);
            canvas.drawCircle(beans.get(i).getPoint().x, beans.get(i).getPoint().y, circleR - perDashLineHeight, circlePaint);
        }
        canvas.drawLine(0, height - perDashLineHeight / 2 - topButtomHeight, width, height - perDashLineHeight / 2 - topButtomHeight, linePaint);


        isDrawYear = false;

    }


    private float logicX(float endX, float startX) {
        float controlX;
        // 设置操作点为线段x/y的一半<br />
        controlX = (endX + startX) * 0.5f;
        return controlX;
    }

    private float logicY(float endY, float startY, int type) {
        float controlY;
        // 设置操作点为线段x/y的一半<br />
        controlY = (endY + startY) * 0.5f + type * LINE_SMOOTHNESS;
        return controlY;
    }
}
